package com.moco.launcher.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.moco.launcher.activity.MainActivity;
import com.moco.launcher.bean.MusicCategoryDataBean;
import com.moco.launcher.fragment.MusicCategoryFragment;

import android.app.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import android.util.Log;

import android.view.ViewGroup;

public class MainFlagmentAdapter extends FragmentPagerAdapter {

	private Vector<Fragment> fragments = new Vector<Fragment>();
	private final FragmentManager fm;

	public MainFlagmentAdapter(Activity activity, FragmentManager fm) {
		super(fm);
		this.fm = fm;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// super.destroyItem(container, position, object);
	}

	public void appendList(ArrayList<Fragment> fragment) {
		if (!fragments.containsAll(fragment) && fragment.size() > 0) {
			fragments.addAll(fragment);
			notifyDataSetChanged();
		}
	}

	public void append(Fragment fragment) {
		if (!fragments.contains(fragment)) {
			fragments.add(fragment);
			notifyDataSetChanged();
		}
	}

	public void remove(int index) {
		for (int i = index; i < fragments.size() - 1; i++) {
			Fragment fragment = fragments.get(i);
			Fragment fragmentEd = fragments.get(i + 1);
			if (fragment instanceof MusicCategoryFragment
					&& fragmentEd instanceof MusicCategoryFragment) {

				MusicCategoryFragment mFragment = (MusicCategoryFragment) fragment;
				MusicCategoryFragment mFragmentEd = (MusicCategoryFragment) fragmentEd;

				MusicCategoryDataBean bean = new MusicCategoryDataBean();

				bean.setnLevel(mFragmentEd.getnLevel());
				bean.setParentID(mFragmentEd.getParentID());
				bean.setRoots(mFragmentEd.getRoots());
				bean.setSaveChildIndex(mFragmentEd.getSaveChildIndex());
				bean.setServiceMsg(mFragmentEd.getServiceMsg());

				mFragment.setServiceMsg(bean);
			}
		}
		Fragment fragment = fragments.remove(fragments.size() - 1);
		FragmentTransaction ft = fm.beginTransaction();
		ft.remove(fragment);
		ft.commitAllowingStateLoss();
		
		fragment.onPause();
		fragment.onStop();
		fragment.onDestroyView();
		fragment.onDestroy();
		fragment.onDetach();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return fragments.size();
	}

	@Override
	public Fragment getItem(int position) {
		return fragments.get(position);
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		Log.d("wwww", "postion==" + position);
		Object object = super.instantiateItem(container, position);
		return object;

	}

	public Fragment get(int playFragmentIndex) {
		return fragments.get(playFragmentIndex);
	}

	public void clear() {
		fragments.clear();
		notifyDataSetChanged();
	}

	/*
	 * public void remove(MusicCategoryFragment musciFragment) { if
	 * (this.fragments != null) {
	 * 
	 * 
	 * FragmentTransaction ft = fm.beginTransaction();
	 * 
	 * //ft.detach(musciFragment); //ft.hide(musciFragment);
	 * 
	 * ft.remove(musciFragment);
	 * 
	 * ft.commit(); ft = null; fm.executePendingTransactions();
	 * 
	 * // setFragments(fragments); } }
	 */
}
